package mianshi;

/**
 * description:
 * author:张腾
 * date:2021-07-18
 */

import niuke.ListNode;

import java.util.HashSet;

/**
 * 编写代码，移除未排序链表中的重复节点。保留最开始出现的节点。
 */
public class _02_01 {
    public ListNode removeDuplicateNodes(ListNode head) {
        HashSet<Integer> exist = new HashSet<>();
        ListNode dum = new ListNode();
        dum.next = head;
        ListNode pre = dum;
        while (head!=null){
            if (exist.contains(head.val)){
                pre.next = head.next;
            }else{
                exist.add(head.val);
                pre = pre.next;
            }
            head = head.next;

        }
        return dum.next;
    }
}
